﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using yiCommerce.Admin.Models.ProductVariants;
using yiCommerce.Admin.Models.Products;
using yiCommerce.Core;
using yiCommerce.Core.Domain.Products;
using yiCommerce.Core.Domain.Securitys;
using yiCommerce.Service.Pictures;
using yiCommerce.Service.Products;
using yiCommerce.Service.Securitys;

namespace yiCommerce.Admin.Controllers
{
    public class ProductController :BaseYiController 
    {

        private IPermissionService _permissionService;
        private IProductSearchService _productSearchService;
        private IProductService _productService;
        private IPictureService _pictureService;
        private IWorkContext _workContext;

        public ProductController(IWorkContext workContext, IPermissionService permissionService, IProductSearchService productSearchService, IProductService productService, IPictureService pictureService)
        {
            _workContext = workContext;
            _permissionService = permissionService;
            _productSearchService = productSearchService;
            _productService = productService;
            _pictureService = pictureService;
        }

        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult List()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageProduct, _workContext.CurrentCustomer.Id))
            {
                return AccessDeniedView();
            }

            var shopId = this.GetShopId();
            var products = _productSearchService.SearchProducts(shopId, 0, 0, string.Empty,ProductSortingEnum.CreatedOn,  0, 0);


            return null;
        }

        public ActionResult Create()
        {
            var model = new ProductModel();
            ViewBag.Model = model;

            return View();
        }

        public ActionResult Edit(int productId)
        {
            //if (!_permissionService.Authorize(StandardPermissionProvider.ManageProduct))
            //{
            //    return AccessDeniedView();
            //}

            var p = _productService.GetProductById(productId);

            var model = new ProductModel()
                {
                    Id = p.Id,
                    ProductName = p.ProductName,
                    ShortDescription = p.ShortDescription,
                    FullDescription = p.FullDescription,

                    ProductPictures = ToProductPictureModels(p.ProductPictures)  ,

                    ProductVariants = ToProductVariantModels(p.ProductVariants),
                    ProductSpecificationAttributes=ToProductSpecificationAttributes(p.ProductSpecificationAttributes),
                };

            ViewBag.Model = model;
            return View("Edit");

        }

        private IList<ProductSpecificationAttributeListItemModel> ToProductSpecificationAttributes(
            ICollection<ProductSpecificationAttribute> productSpecificationAttributes)
        {
            if (productSpecificationAttributes == null)
            {
                return new List<ProductSpecificationAttributeListItemModel>();
            }
            else
            {
                return productSpecificationAttributes.Select(c => new ProductSpecificationAttributeListItemModel()
                    {
                        SpecificationAttributeName = c.SpecificationAttributeOption.SpecificationAttribute.Name,
                        SpecificationAttributeOptionName = c.SpecificationAttributeOption.Name
                    }).ToList();
            }
        }

        public IList<ProductPictureModel> ToProductPictureModels(ICollection<ProductPicture> productPictures)
        {

            if (productPictures == null)
            {
                return new List<ProductPictureModel>();
            }
            else
            {
                return productPictures.Select(c => new ProductPictureModel()
                {
                    PictureUrl = _pictureService.GetPictureUrl(c.Picture.Id,100,true),
                    PictureId = c.PictureId,
                    DisplayOrder = c.DisplayOrder,
                }).ToList();
            }
        }

        public IList<ProductVariantModel> ToProductVariantModels(ICollection<ProductVariant> productVariants)
        {

            if (productVariants == null)
            {
                return new List<ProductVariantModel>();
            }
            else
            {
                return productVariants.Select(c => new ProductVariantModel()
                    {
                        Id = c.Id,
                        Name = c.Name,
                        Sku = c.Sku,
                        Price = c.Price,
                        DisplayOrder = c.DisplayOrder,
                        Published = c.Published,

                    }).ToList();
            }
        }
    }
}
